package com.dkakunsi.monev.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dkakunsi.monev.dao.IndikatorDao;
import com.dkakunsi.monev.dao.KegiatanDao;
import com.dkakunsi.monev.dao.RealisasiDanaDao;
import com.dkakunsi.monev.dao.RealisasiDao;
import com.dkakunsi.monev.dao.RealisasiIndikatorDao;
import com.dkakunsi.monev.entity.Indikator;
import com.dkakunsi.monev.entity.Kegiatan;
import com.dkakunsi.monev.entity.Progress;
import com.dkakunsi.monev.entity.Realisasi;
import com.dkakunsi.monev.entity.RealisasiDana;
import com.dkakunsi.monev.entity.RealisasiIndikator;
import com.dkakunsi.monev.util.MonevDataException;

@Service("realisasiService")
@Transactional(readOnly=true)
public class RealisasiService extends MonevAbstractService {
	@Autowired private RealisasiDao realisasiDao;
	@Autowired private RealisasiDanaDao realisasiDanaDao;
	@Autowired private RealisasiIndikatorDao realisasiIndikatorDao;
	@Autowired private KegiatanDao kegiatanDao;
	@Autowired private IndikatorDao indikatorDao;
	
	@Transactional(readOnly=false)
	public Kegiatan realisasi(RealisasiDana r) throws MonevDataException {
		realisasiDanaDao.save(r);

		Realisasi lastMonth = realisasiDao.get(r.getKegiatan(), r.getBulan() - 1);
		Realisasi now = realisasiDao.get(r.getKegiatan(), r.getBulan());
		now.setDana(lastMonth.getDana() + r.getJumlah());
		
		return realisasi(now);
	}
	
	@Transactional(readOnly=false)
	public Kegiatan realisasi(RealisasiIndikator r) throws MonevDataException {
		realisasiIndikatorDao.save(r);

		double progres = 0;
		Indikator i = r.getIndikator();
		for(RealisasiIndikator ri : realisasiIndikatorDao.get(i)) {
			progres += ri.getJumlah();
		}
		i.setProgres(progres);
		indikatorDao.save(i);
		
		progres = 0;
		int counter = 0;
		Realisasi now = realisasiDao.get(i.getKegiatan(), r.getBulan());
		for(Indikator in : indikatorDao.get(i.getKegiatan())) {
			progres += in.presentaseProgres();
			counter++;
		}
		now.setFisik(progres / counter);
				
		return realisasi(now);
	}

	@Transactional(readOnly=false)
	public Kegiatan realisasi(Realisasi r) throws MonevDataException {
		realisasiDao.save(r);

		Kegiatan k = r.getKegiatan();
		Progress p = new Progress(r.getFisik(), r.getDana());
		k.setProgres(p);

		kegiatanDao.save(k);
		return k;
	}

	public List<Realisasi> getRealisasi(Kegiatan k) throws MonevDataException {
		return realisasiDao.get(k);
	}
	
	public List<RealisasiIndikator> getRealisasiIndikator(Kegiatan k) throws MonevDataException {
		return realisasiIndikatorDao.get(k);
	}
	
	public List<RealisasiIndikator> getRealisasiIndikator(Kegiatan k, int bulan) throws MonevDataException {
		return realisasiIndikatorDao.get(k, bulan);
	}
	
	public RealisasiIndikator getRealisasiIndikator(Indikator i, int bulan) throws MonevDataException {
		return realisasiIndikatorDao.get(i, bulan);
	}

	public List<RealisasiDana> getRealisasiDana(Kegiatan k) throws MonevDataException {
		return realisasiDanaDao.get(k);
	}
	
	public RealisasiDana getRealisasiDana(Kegiatan k, int bulan) throws MonevDataException {
		return realisasiDanaDao.get(k, bulan);
	}
}
